import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
import { PreferenceService, CommonCommands } from '@theia/core/lib/browser';
import { WorkspaceCommands } from '@theia/workspace/lib/browser';
import { AbstractViewContribution } from '@theia/core/lib/browser';
import { CommandRegistry, Command, MenuModelRegistry } from '@theia/core';
import { AppleWidget } from './apple-widget';

export const AppleWidgetCommand: Command = { id: 'app-widget:command' };

/**
 * Contribution of the `AppleWidget`
 */
@injectable()
export class AppleWidgetViewContribution extends AbstractViewContribution<AppleWidget> {
    @inject(PreferenceService)
    protected readonly preferenceService: PreferenceService;
    @inject(MenuModelRegistry) protected menuModel: MenuModelRegistry;

    constructor() {
        super({
            widgetId: AppleWidget.ID,
            widgetName: AppleWidget.LABEL,
            defaultWidgetOptions: { 
                area: 'left',
                rank: 1  // 设置较高的优先级
            },
            toggleCommandId: AppleWidgetCommand.id,
        });
        console.log('AppleWidgetViewCo========ntribution');
    }

    @postConstruct()
    protected init(): void {
        this.doInit();
    }

    protected async doInit(): Promise<void> {
        try {
            await this.preferenceService.ready;
            await this.shell.initialized;

            this.menuModel.unregisterMenuAction(CommonCommands.NEW_UNTITLED_TEXT_FILE);
            this.menuModel.unregisterMenuAction(CommonCommands.NEW_FILE);
            this.menuModel.unregisterMenuAction(CommonCommands.CLOSE_MAIN_TAB);
            this.menuModel.unregisterMenuAction(WorkspaceCommands.CLOSE);

        } catch (error) {
            console.error('Failed to initialize AppleWidgetViewContribution:', error);
        }
    }

    registerMenus(menus: MenuModelRegistry): void {
        super.registerMenus(menus);
    }

    registerCommands(commands: CommandRegistry): void {
        commands.registerCommand(AppleWidgetCommand, {
            execute: () => super.openView({ activate: false, reveal: true })
        });
    }
}
